package co.fitstart.admin.web.controller.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.DateFormatUtils;

import co.fitstart.admin.entity.report.ReportPrepare;
import co.fitstart.admin.entity.report.SiteHour;
import co.fitstart.admin.entity.report.SiteHourStat;
import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.ReportService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 线下1V1平均上课率
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/individual-hour-reports")
@MenuSetting("menu-report")
public class IndividualHourReportController {
    @Autowired
    private SiteService siteService;
    
    @Autowired
    private ReportService reportService;
    
    @Autowired
    private PrincipalService principalService;
    
    /**
     * 
     * @param login
     * @param siteId
     * @param date
     * @param period
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-individual-hour-report")
    public ModelAndView list(@Secure Login login,
                             @RequestParam(name = "site", required = false) Long siteId,
                             @RequestParam(name = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                             @RequestParam(name = "period", required = false, defaultValue = "day") String period) {
        ModelAndView modelAndView = new ModelAndView("report/individual-hour-report-list");
        
        Site site = null;
        if(!login.hasSite() && siteId != null) {
            site = siteService.load(siteId);
        }else if (login.hasSite()){
            site = login.getSite();
        }
        
        if(date == null) {
            date = new Date();
        }
        
        if(!"week".equals(period) && !"month".equals(period)) {
            period = "day";
        }
        
        List<SiteHour> siteHourList = new ArrayList<>();
        int hour = 0;
        List<SiteHourStat> statList = new ArrayList<>();
        
        if(site == null && (login.isAreaManager() || login.isAreaServer())) {
            Principal principal = principalService.load(Principal.class, login.getId());
            List<Site> sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                siteHourList = reportService.listSiteHour(CourseType.INDIVIDUAL, sites, date, period);
                hour = reportService.getSiteHour(CourseType.INDIVIDUAL, sites, date, period);
                statList = reportService.listSiteHourStat(CourseType.INDIVIDUAL, sites, date, period);
            }
        } else {
            siteHourList = reportService.listSiteHour(CourseType.INDIVIDUAL, site, date, period);
            if(site == null) {
                hour = reportService.getSiteHour(CourseType.INDIVIDUAL, date, period);
            }
            statList = reportService.listSiteHourStat(CourseType.INDIVIDUAL, site, date, period);
        }
        modelAndView.addObject("siteHourList", CollectionUtils.constructMapValueFromList(siteHourList, "siteName"));
        modelAndView.addObject("hour", hour);
        modelAndView.addObject("statList", statList);
        
        if(site != null) {
            modelAndView.addObject("selectSite", site);
        }
        modelAndView.addObject("currentDate", date);
        ReportPrepare reportPrepare = new ReportPrepare(site, date, period);
        modelAndView.addAllObjects(reportPrepare.describeReport());
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("report/special-hour-report-export");
        
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param siteId
     * @param beginDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login,
                                         @RequestParam(value = "site", required=false) Long siteId,
                                         @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                         @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("individual-hour-report.xls"));
        
        Site site = null;
        if(login.hasSite()) {
            site = login.getSite();
        } else if(siteId != null) {
            site = siteService.load(siteId);
        }
        
        if(beginDate == null || endDate == null) {
            beginDate = new Date();
            endDate = new Date();
        }
        
        List<SiteHour> siteHourList = new ArrayList<>();
        List<Site> sites = new ArrayList<>();
        int hour = 0;
        List<SiteHourStat> statList = new ArrayList<>();
        List<CourseSchedule> scheduleList = new ArrayList<>();
        
        if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                if(siteId == null) {
                    siteHourList = reportService.listSiteHour(CourseType.INDIVIDUAL, sites, beginDate, endDate);
                    hour = reportService.getSiteHour(CourseType.INDIVIDUAL, sites, beginDate, endDate);
                    statList = reportService.listSiteHourStat(CourseType.INDIVIDUAL, sites, beginDate, endDate);
                    scheduleList = reportService.listCourseSchedule(CourseType.INDIVIDUAL, sites, beginDate, endDate);
                } else if(site != null && sites.contains(site)) {
                    siteHourList = reportService.listSiteHour(CourseType.INDIVIDUAL, site, beginDate, endDate);
                    sites.add(site);
                    statList = reportService.listSiteHourStat(CourseType.INDIVIDUAL, site, beginDate, endDate);
                    scheduleList = reportService.listCourseSchedule(CourseType.INDIVIDUAL, site, beginDate, endDate);
                }
            }
        } else {
            siteHourList = reportService.listSiteHour(CourseType.INDIVIDUAL, site, beginDate, endDate);
            if(site == null) {
                sites = siteService.loadAll();
                hour = reportService.getSiteHour(CourseType.INDIVIDUAL, beginDate, endDate);
            } else {
                sites.add(site);
            }
            statList = reportService.listSiteHourStat(CourseType.INDIVIDUAL, site, beginDate, endDate);
            scheduleList = reportService.listCourseSchedule(CourseType.INDIVIDUAL, site, beginDate, endDate);
        }
        
        if(site == null) {
            modelAndView.addObject("hour", hour);
        }
        modelAndView.addObject("statList", statList);
        modelAndView.addObject("scheduleList", scheduleList);
        modelAndView.addObject("beginDate", DateFormatUtils.format(beginDate, "yyyy-MM-dd"));
        modelAndView.addObject("endDate", DateFormatUtils.format(endDate, "yyyy-MM-dd"));
        modelAndView.addObject("sites", sites);
        modelAndView.addObject("siteHourList", CollectionUtils.constructMapValueFromList(siteHourList, "siteName"));
        
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
    }
}
